<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;

class IndexController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('check_admin_login');
    }

    public function index()
    {
        $yesterday_day = date('Y-m-d',strtotime("-1 day"));
        $yesterday_day_start_time = strtotime($yesterday_day.' 00:00:00');
        $yesterday_day_end_time = strtotime($yesterday_day.' 23:59:59');

        $result = [];
        //卖书订单
        //待发货订单数量
        $order_waitting_send_num = DB::table('orders')->where('state',1)->count();
        //昨日退款订单数量
        $yesterday_refund_num = DB::table('orders')->where('state',-1)->where('refund_status',2)->where('cancel_at','>=',$yesterday_day_start_time)->where('cancel_at','<=',$yesterday_day_end_time)->count();
        //昨日订单金额
        $yesterday_order_price = DB::table('orders')->where('created_at','>=',$yesterday_day_start_time)->where('created_at','<=',$yesterday_day_end_time)->sum('price');
        //昨日订单数
        $yesterday_order_num = DB::table('orders')->where('created_at','>=',$yesterday_day_start_time)->where('created_at','<=',$yesterday_day_end_time)->count();
        //订单总数
        $total_order_num  = DB::table('orders')->count();
        //微信支付订单总数
        $total_order_num_wechat  = DB::table('orders')->where('pay_type',2)->count();
        //余额支付订单总数
        $total_order_num_account  = DB::table('orders')->where('pay_type',1)->count();
        //总订单金额
        $total_price = DB::table('orders')->sum('price');
        //微信支付总订单金额
        $total_price_wechat = DB::table('orders')->where('pay_type',2)->sum('price');
        //余额支付总订单金额
        $total_price_account = DB::table('orders')->where('pay_type',1)->sum('price');

        $result['book_order'] = [
            'order_waitting_send_num'   => $order_waitting_send_num, //待发货订单数量
            'yesterday_refund_num'      => $yesterday_refund_num,  //昨日退款订单数量
            'yesterday_order_price'     => $yesterday_order_price,  //昨日订单金额
            'yesterday_order_num'       => $yesterday_order_num,   //  昨日订单数
            'total_order_num'           => $total_order_num,  //订单总数
            'total_order_num_wechat'    => $total_order_num_wechat,  //微信支付订单总数
            'total_order_num_account'   => $total_order_num_account,  //余额支付订单总数
            'total_price'               => $total_price,  //总订单金额
            'total_price_wechat'        => $total_price_wechat,  //微信支付总订单金额
            'total_price_account'       => $total_price_account,  //余额支付总订单金额
        ];

        //收书订单
        //已提交审核订单数量
        $sell_order_submit_check_num = DB::table('sell_orders')->where('state',1)->count();
        //待平台确认收货
        $sell_order_confirm_num = DB::table('sell_orders')->where('state',3)->count();
        //昨日订单数
        $yesterday_sell_order_num = DB::table('sell_orders')->where('created_at','>=',$yesterday_day_start_time)->where('created_at','<=',$yesterday_day_end_time)->count();
        //昨日取消订单数
        $yesterday_sell_order_cancel_num = DB::table('sell_orders')->where('state',-2)->where('cancel_time','>=',$yesterday_day_start_time)->where('cancel_time','<=',$yesterday_day_end_time)->count();
        //订单总数
        $total_sell_order_num  = DB::table('sell_orders')->where('state','!=',0)->count();
        //审核完成订单总数
        $total_sell_order_check_num  = DB::table('sell_orders')->where('state',5)->count();
        //总打款金额
        $total_sell_order_price = DB::table('sell_orders')->where('state',5)->sum('actual_price');

        $result['sell_order'] = [
            'sell_order_submit_check_num'       => $sell_order_submit_check_num,  //已提交审核订单数量
            'sell_order_confirm_num'            => $sell_order_confirm_num,//待平台确认收货
            'yesterday_sell_order_num'          => $yesterday_sell_order_num,//昨日订单数
            'yesterday_sell_order_cancel_num'   => $yesterday_sell_order_cancel_num,//昨日取消订单数
            'total_sell_order_num'              => $total_sell_order_num,//订单总数
            'total_sell_order_check_num'        => $total_sell_order_check_num,//审核完成订单总数
            'total_sell_order_price'            => $total_sell_order_price,//总打款金额
        ];


        //退回订单
        //待处理退回订单
        $waitting_handle_back_order_num = DB::table('back_orders')->where('state',1)->count();
        //昨日申请退回订单数
        $yesterday_apply_back_order_num = DB::table('back_orders')->where('state',1)->where('created_at','>=',$yesterday_day_start_time)->where('created_at','<=',$yesterday_day_end_time)->count();
        //申请退回订单总数
        $total_back_order_num = DB::table('back_orders')->where('state','>',0)->count();
        //已退回订单数
        $success_back_order_num = DB::table('back_orders')->where('state',2)->count();

        $result['back_order'] = [
            'waitting_handle_back_order_num'    => $waitting_handle_back_order_num,//待处理退回订单
            'yesterday_apply_back_order_num'    => $yesterday_apply_back_order_num,//昨日申请退回订单数
            'total_back_order_num'              => $total_back_order_num,//申请退回订单总数
            'success_back_order_num'            => $success_back_order_num,//已退回订单数
        ];

        //用户
        //昨日提现人数
        $yesterday_get_money_num = DB::table('user_account_records')->where('type',1)->where('state',1)->where('is_get_money',1)->where('created_at','>=',$yesterday_day.' 00:00:00')->where('created_at','<=',$yesterday_day.' 23:59:59')->count();
        //昨日提现金额
        $yesterday_get_money = DB::table('user_account_records')->where('type',1)->where('state',1)->where('is_get_money',1)->where('created_at','>=',$yesterday_day.' 00:00:00')->where('created_at','<=',$yesterday_day.' 23:59:59')->sum('account');
        //昨日提现失败次数
        $yesterday_get_money_error_num = DB::table('user_account_records')->where('type',1)->where('is_get_money',1)->where('state',2)->where('updated_at','>=',$yesterday_day.' 00:00:00')->where('updated_at','<=',$yesterday_day.' 23:59:59')->count();
        //累计提现失败次数
        $total_get_money_error_num = DB::table('user_account_records')->where('type',1)->where('is_get_money',1)->where('state',2)->count();
        //当前可提现金额
        $money_total = DB::table('users')->where('user_type',1)->sum('account');
        //账户有余额人数
        $have_money_user_num = DB::table('users')->where('user_type',1)->where('account','>',0)->count();
        //累计提现人数
        $total_get_money_num = DB::table('user_account_records')->where('state',1)->where('type',1)->where('is_get_money',1)->count();
        //累计提现总额
        $total_money = DB::table('user_account_records')->where('state',1)->where('type',1)->where('is_get_money',1)->sum('account');

        $result['user'] = [
            'yesterday_get_money_num'       => $yesterday_get_money_num, //昨日提现人数
            'yesterday_get_money'           => $yesterday_get_money,//昨日提现金额
            'yesterday_get_money_error_num' => $yesterday_get_money_error_num,//昨日提现失败次数
            'total_get_money_error_num'     => $total_get_money_error_num,//累计提现失败次数
            'money_total'                   => $money_total,//当前可提现金额
            'have_money_user_num'           => $have_money_user_num,//账户有余额人数
            'total_get_money_num'           => $total_get_money_num,//累计提现人数
            'total_money'                   => $total_money,//累计提现总额
        ];
        return $this->ok($result);
    }
}
